/**
 * [OrderController description]
 */
OrderController = function () {

    // 获取订单列表
    this.getOrderStatisticsList = function(param, resetFunc) {
        var serverUrl = App.serverUrl +　"app/order/getOrderCountList.do";
        var sucFunc = function (result) {
            $('#orderPage #orderPageNewOrder').css({display:'none'});
            if (result && result.success) {
                // 服务器返回成功
                var pageData = result.resultObject;

                if (param.pageNo === 1&&pageData.nextPage!==0) {
                    // 第一页，清除列表缓存数据
                    orderParam.cache = {};
                    initAddMoreItem("#orderPage .page-body");
                }
                //渲染数据
                initOrderList({
                    parent:'#orderPage .tab1',
                    data:pageData.pageData,
                    isAppend:param.pageNo > 1
                });

                if (pageData.nextPage > 0) {
                    // TODO 显示更多
                    resetFunc && resetFunc();
                } else {
                    // TODO 没有更多了
                    resetFunc && resetFunc(true);
                    disableAddMoreItem($("#orderPage .page-body"));
                    if ($("#orderPage .tab1").html()) {
                        $("#orderPage .page-body").append('<div style="margin-top: -2.2rem;" class="no-data-wrap no-more-order"><p>没有更多</p></div>');
                    }

                }
            } else {
                DialogUtil.dialogWarn(result.msg);
            }
            initRefresh();
        }

        //异常处理
        var errorFunc = function() {
            resetFunc && resetFunc();
            $("#orderPage .page-body .infinite-scroll-preloader").addClass("hidden");
        }

        CommonUtil.sendJsonpRequest({
            url: serverUrl,
            params: param,
            success: sucFunc,
            error: errorFunc
        });

    };

    // 获取订单详情
    this.getExceptionOrderList = function(param, resetFunc) {
        var serverUrl = App.serverUrl +　"app/order/getErrorOrderList.do";
        var sucFunc = function (result) {
            if (result && result.success) {
                // 服务器返回成功
                var pageData = result.resultObject;
                if (param.pageNo === 1&&pageData.nextPage!==0) {
                    // 第一页，清除列表缓存数据
                    orderParam.cache = {};
                    initAddMoreItem("#orderPage .page-body");
                }

                //渲染数据
                initOrderList({
                    parent:'#orderPage .tab2',
                    data:pageData.pageData,
                    isAppend:param.pageNo > 1
                });

                if (pageData.nextPage > 0) {
                    // TODO 显示更多
                    resetFunc && resetFunc();
                } else {
                    // TODO 没有更多了
                    resetFunc && resetFunc(true);
                    disableAddMoreItem($("#orderPage .page-body"));
                    if ($("#orderPage .tab2").html()) {
                        $("#orderPage .page-body").append('<div style="margin-top: -2.2rem;" class="no-data-wrap no-more-order"><p>没有更多</p></div>');
                    }
                }
            } else {
                DialogUtil.dialogWarn(result.msg);
            }
            resetFunc && resetFunc();
            initRefresh();
        }

        //异常处理
        var errorFunc = function() {
            resetFunc && resetFunc();
            $("#orderPage .page-body .infinite-scroll-preloader").addClass("hidden");
        }

        CommonUtil.sendJsonpRequest({
            url: serverUrl,
            params: param,
            success: sucFunc,
            error: errorFunc
        });
    };

    //获取boss账号开通时间，并初始化日期控件
    this.date = function(initDateFuntion) {
        var serverUrl = App.serverUrl +　"app/order/getBossAccount.do";
        var sucFunc = function (result) {
            if (result && result.success) {
                // 服务器返回成功
                var bossAccount = result.resultObject;
                if (bossAccount && bossAccount.createTime) {
                    orderParam.bossOpenDate = new Date(parseInt(bossAccount.createTime));
                }
            }
            initDateFuntion();
        }

        CommonUtil.sendJsonpRequest({
            url: serverUrl,
            success: sucFunc
        });
    };

    //数据渲染
    /*{parent:'#orderStatisticsListDiv',data:[],isAppend}*/
    var initOrderList = function(param){
        var $parent = $(param.parent);
        //删除【暂无订单】展示信息
        $parent.parent().find('.no-data-wrap').remove();
        $(".tab-wrap-last").empty();
        $('.tab-wrap-last').show();
        if(!param.isAppend){
            $parent.empty();
            if (param.data.length == 0) {
                $parent.parent().append('<div class="no-data-wrap">'
                        + '<div class="no-data"></div>'
                        + '<p>暂无订单</p>'
                    + '</div>');
                $('.tab-wrap-last').hide();
                return;
            }

        }

        var $days = $parent.find('.item'),
        $lastDay = $($days[$days.length-1]);
        var initDataListDom = function(data){
            var template = '';
            for(var i=0,length=data.length;i<length;i++){
                template+='<li orderId="' + data[i].orderNo + '" orderType="'+data[i].orderType+'" shopId="'+data[i].shopId+'" orderStatus="'+data[i].orderStatus+'">'
                    +'<span class="left">';
                    switch(data[i].payType){
                        case '1':
                        template+='<p class="orange-gradient zhk-detail_cash"></p>';
                        break;
                        case '2':
                        template+='<p class="blue-gradient zhk-detail_zfb"></p>';
                        break;
                        case '3':
                        template+='<p class="purple-gradient zhk-detail_vip"></p>';
                        break;
                        case '4':
                        template+='<p class="deepBlue-gradient zhk-detail_yl"></p>';
                        break;
                        case '5':
                        template+='<p class="green-gradient zhk-detail_wx"></p>';
                        break;
                        default:
                        template+='<p class="blue-gradient"></p>';
                        break;
                    }
                    template+='</span>'
                    +'<span class="center">'
                        +'<p class="name">'+data[i].shopName+'</p>'
                        +'<p class="date">'+data[i].tradeDate+'</p>'
                    +'</span>'
                    +'<span class="right">'
                        +'<p class="sprice"><i>¥</i>'+data[i].receiveMoney+'</p>';
                        switch(data[i].payStatus){
                            case '1':
                                template+='<p class="state">已收款</p>';
                            break;
                            case '3':
                                template+='<p class="state">退款中</p>';
                            break;
                            case '2':
                                template+='<p class="state">已退款</p>';
                            break;
                            case '4':
                                template+='<p class="state">撤销中</p>';
                            break;
                            case '5':
                                template+='<p class="state">已撤销</p>';
                            break;
                            case '6':
                                template+='<p class="state">外卖已完成</p>';
                            break;
                            case '7':
                                template+='<p class="state">外卖已退款</p>';
                            break;
                            case '8':
                                template+='<p class="state">预约点餐已取餐</p>';
                            break;
                            case '9':
                                template+='<p class="state">预约点餐已退款</p>';
                            break;
                            case '10':
                                template+='<p class="state">预约点餐待制作</p>';
                            break;
                            case '11':
                                template+='<p class="state">预约点餐待取餐</p>';
                            break;
                            case '12':
                                template+='<p class="state">收款失败</p>';
                            break;
                            case '13':
                                template+='<p class="state">收款异常</p>';
                            break;
                            default:
                                template+='<p class="state">未知</p>';
                            break;
                        }

                    template+='</span>'
                +'</li>';
            }
            return template;
        };
        var appendOldDayDom = function(lastDay,data){
            var template='';
            template +=initDataListDom(data);
            lastDay.find('.list').append(template);
        };
        var newDayDom = function(data){
            var template='<div class="item" date='+data.date+'>'
                            +'<div class="title">'
                                +'<div class="left">'+data.date+'</div>';
                            if(!!data.count&&!!data.payAmt){
                                template+='<div class="right">'
                                    +'<span class="num">'+data.count+'笔</span>'
                                    // +'<span class="sprice"><i class="zhk-detail_gj"></i>总计：¥ '+data.payAmt+'</span>'
                                    +'<span class="sprice"><i class="zhk-detail_gj"></i>总计：</span>'
                                +'</div>';
                            }
            template+='</div><ul class="list">';
            template+= initDataListDom(data.orderList);
            template+='</ul></div>';
            return template;
        }
        var tmp='';
        for(var i=0,length=param.data.length;i<length;i++){
            if($lastDay!==""&&$lastDay.length !==0&&$lastDay.attr('date')===param.data[i].date){
                appendOldDayDom($lastDay,param.data[i].orderList);
            }else{
                tmp+=newDayDom(param.data[i]);
            }
        }
        $parent.append(tmp);
    }

    //初始化下滑日期行固定
    var initRefresh = function() {
        var htop = $(".header").height()+$(".tab-wrap").height();
        var maxA = [];
        $(".tab-wrap-last").empty();
        for(var i=0;i<$(".tab1 .item").length;i++){
            if($(".tab1 .item").eq(i).offset().top-htop<=2){
                maxA.push(i)
            }
        }
        $('.tab-wrap-last').append($(".tab1 .item").eq(maxA[maxA.length-1]).find(".title").clone(true))
        $(".tab-wrap-last").css({top:htop});
    }


};
